package com.example.cinema.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.bumptech.glide.Glide;
import com.example.cinema.BR;
import com.example.cinema.R;

import java.util.List;

public class Blank2GridAdapter<T> extends BaseAdapter {

    private List<T> data;
    private Context context;
    private int layoutid;
    private LayoutInflater inflater;
    public Blank2GridAdapter(List<T> data, Context context, int layoutid) {
        this.data = data;
        this.context = context;
        this.layoutid = layoutid;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private ItemView itemView;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null)
        {
            ViewDataBinding dataBinding = DataBindingUtil.inflate(inflater,layoutid,parent,false);
            convertView = dataBinding.getRoot();
            itemView = new ItemView();
            itemView.dataBinding = dataBinding;
            convertView.setTag(itemView);
        }else {
            itemView = (ItemView) convertView.getTag();
            ImageView imageView = itemView.dataBinding.getRoot().findViewById(R.id.image);
            Glide.with(imageView).load(data.get(position)).into(imageView);
        }
        Log.i("HHH","渲染");
        notifyDataSetChanged();

        return convertView;
    }


    class ItemView{
        ViewDataBinding dataBinding;
    }
}
